<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1-4 基于闭包实现获取元素样式</title>
  <style>
    .box {
      width: 200px;
      height: 200px;
    }
  </style>
</head>

<body>
  <div class="box">box...</div>

  <script>
    function getElemStyle(el, attr) {
      // 验证getComputedStyle是否存在，方法很多，自己身边选一个就可以了
      if ("getComputedStyle" in window) {
        return window.getComputedStyle(el)[attr];
      }
      return el.currentStyle[attr];
    }
    var box_width = getElemStyle(document.querySelector('.box'), "width");
    console.log(box_width);

    // 闭包高阶应用：惰性函数可以优化
  </script>
</body>

</html>